package com.tmt.paymentcenter.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.tmt.paymentcenter.web.configure.security.bean.ClientResources;
import com.tmt.paymentcenter.web.configure.security.bean.TmtClientSessionRegistry;
import com.tmt.paymentcenter.web.vo.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/external")
public class ExternalController extends BaseController {

    @Autowired
    private TmtClientSessionRegistry sessionRegistry;

    @Autowired
    private ClientResources oauth2Client;

    @ResponseBody()
    @RequestMapping(value = "/logout/{clientId}/{userId}")
    public JSONObject userLogout(HttpServletRequest request,
                                 @PathVariable("userId") Long userId,
                                 @PathVariable("clientId") String clientId) {

        if (!clientId.equals(oauth2Client.getClient().getClientId())){
            //指定用户已经登出了。
            sessionRegistry.userLogout(userId, false);
        }
        return HttpResult.success().toJSONObject();
    }
}
